package com.allwees.bs.c.module.order.repository;

import com.allwees.bs.c.module.base.repository.BaseRepository;
import com.allwees.bs.c.module.order.entity.OrderEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Set;

public interface OrderRepository extends BaseRepository<OrderEntity, Integer> {
    @Query("SELECT o FROM OrderEntity o WHERE o.state = 'CREATED' AND o.paymentExpiredAt <= :time")
    List<OrderEntity> findUnPaidOrderList(@Param("time") Date time);

    @Query("SELECT o FROM OrderEntity o WHERE o.state = 'CREATED' AND o.paymentExpiredAt > :paymentExpiredAtFrom AND o.paymentExpiredAt < :paymentExpiredAtTo")
    List<OrderEntity> findUnPaidUnExpireOrderList(@Param("paymentExpiredAtFrom") Date paymentExpiredAtFrom, @Param("paymentExpiredAtTo") Date paymentExpiredAtTo);

    @Query("SELECT uuid FROM OrderEntity o WHERE o.state = 'WAIT_SHIP' AND synced = false AND o.paymentAt <= :time")
    List<String> findCanSyncOrders(@Param("time") Date time);

    @Query("SELECT o FROM OrderEntity o")
    List<OrderEntity> findAllOrders();

    @Query("SELECT o FROM OrderEntity o WHERE o.shippingWaitAt < :shippingDeadLine AND o.state = 'WAIT_SHIP' AND o.type != 2")
    List<OrderEntity> findOutOvertimeShippingOrders(@Param("shippingDeadLine") Date shippingDeadLine);

    @Query("SELECT o FROM OrderEntity o WHERE o.createdAt > :afterTime")
    List<OrderEntity> findLatestOrder(@Param("afterTime") Date afterTime, Pageable pageable);

    @Query("SELECT o.userUuid FROM OrderEntity o WHERE o.uuid in :uuids")
    Set<String> getUserUuidByUuids(@Param("uuids") Set<String> uuids);

    @Query("SELECT count(o) FROM OrderEntity o WHERE o.payed = true")
    int getUserPayedOrderCount(String userUuid);

    OrderEntity getByUuid(String uuid);

    OrderEntity getByNo(String no);

    List<OrderEntity> findAllByUserUuid(String userUuid);
}



